<?php
class ControllerRestaurantBooking extends Controller {
	private $error = array();

  	public function index() {
		$this->load->language('restaurant/booking');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('restaurant/booking');

    	$this->getList();
  	}

  	public function update() {
		$this->load->language('restaurant/booking');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('restaurant/booking');
    	
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_restaurant_booking->editBooking($this->request->get['booking_id'], $this->request->post);
	  		
			$this->session->data['success'] = $this->language->get('text_success');
	  
			$url = $this->url();
			
			$this->redirect($this->url->link('restaurant/booking', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}
		
    	$this->getForm();
  	}
	
  	public function delete() {
		$this->load->language('restaurant/booking');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('restaurant/booking');

    	if (isset($this->request->post['selected']) && ($this->validateDelete())) {
			foreach ($this->request->post['selected'] as $booking_id) {
				$this->model_restaurant_booking->deleteBooking($booking_id);
			}

			$this->session->data['success'] = $this->language->get('text_success');

			$url = $this->url();

			$this->redirect($this->url->link('restaurant/booking', 'token=' . $this->session->data['token'] . $url, 'SSL'));
    	}

    	$this->getList();
  	}

  	private function getList() {
		if (isset($this->request->get['filter_booking_id'])) {
			$filter_booking_id = $this->request->get['filter_booking_id'];
		} else {
			$filter_booking_id = null;
		}

		if (isset($this->request->get['filter_customer'])) {
			$filter_customer = $this->request->get['filter_customer'];
		} else {
			$filter_customer = null;
		}

		if (isset($this->request->get['filter_email'])) {
			$filter_email = $this->request->get['filter_email'];
		} else {
			$filter_email = null;
		}

		if (isset($this->request->get['filter_telephone'])) {
			$filter_telephone = $this->request->get['filter_telephone'];
		} else {
			$filter_telephone = null;
		}
		
		if (isset($this->request->get['filter_booking_status_id'])) {
			$filter_booking_status_id = $this->request->get['filter_booking_status_id'];
		} else {
			$filter_booking_status_id = null;
		}
		
		if (isset($this->request->get['filter_date_booked'])) {
			$filter_date_booked = $this->request->get['filter_date_booked'];
		} else {
			$filter_date_booked = null;
		}
				
		if (isset($this->request->get['filter_date_added'])) {
			$filter_date_added = $this->request->get['filter_date_added'];
		} else {
			$filter_date_added = null;
		}

		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'rb.booking_status_id';
		}

		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}
		
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
				
		$url = $this->url();

  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => '',
      		'separator' => ' :: '
   		);

		$this->data['ticket'] = $this->url->link('restaurant/booking/ticket', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['delete'] = $this->url->link('restaurant/booking/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');

		$this->data['bookings'] = array();

		$data = array(
			'filter_booking_id'      => $filter_booking_id,
			'filter_customer'	     => $filter_customer,
			'filter_email'	     	 => $filter_email,
			'filter_telephone'	     => $filter_telephone,
			'filter_booking_status_id' => $filter_booking_status_id,
			'filter_date_booked'           => $filter_date_booked,
			'filter_date_added'      => $filter_date_added,
			'sort'                   => $sort,
			'order'                  => $order,
			'start'                  => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit'                  => $this->config->get('config_admin_limit')
		);

		$this->load->model('setting/booking_status');

    	$this->data['booking_statuses'] = $this->model_setting_booking_status->getBookingStatuses();
		
		$booking_total = $this->model_restaurant_booking->getTotalBookings($data);

		$results = $this->model_restaurant_booking->getBookings($data);

    	foreach ($results as $result) {
			$action = array();
						
			$action[] = array(
				'text' => $this->language->get('text_view'),
				'href' => $this->url->link('restaurant/booking/update', 'token=' . $this->session->data['token'] . '&booking_id=' . $result['booking_id'] . $url, 'SSL')
			);
			
			$status = "";
			foreach ($this->data['booking_statuses'] as $booking_status){
				if($result['booking_status_id'] == $booking_status['booking_status_id']){
					$status = $booking_status['name'];
					break;
				}
			}
			
			$this->data['bookings'][] = array(
				'booking_id'    => $result['booking_id'],
				'customer_id'   => $result['customer_id'],
				'customer_name' => $result['firstname'] . ' ' . $result['lastname'],
				'email' 		=> $result['email'],
				'telephone' 	=> $result['telephone'],
				'status'        => $status,
				'date_booked'   => date($this->language->get('date_format_short'), strtotime($result['date_booked'])),
				'date_added'    => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
				'selected'      => isset($this->request->post['selected']) && in_array($result['booking_id'], $this->request->post['selected']),
				'action'        => $action
			);
		}

		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_no_results'] = $this->language->get('text_no_results');
		$this->data['text_abandoned_bookings'] = $this->language->get('text_abandoned_bookings');

		$this->data['column_booking_id'] = $this->language->get('column_booking_id');
    	$this->data['column_customer'] = $this->language->get('column_customer');
    	$this->data['column_email'] = $this->language->get('column_email');
    	$this->data['column_telephone'] = $this->language->get('column_telephone');
    	$this->data['column_status'] = $this->language->get('column_status');
		$this->data['column_date_booked'] = $this->language->get('column_date_booked');
		$this->data['column_date_added'] = $this->language->get('column_date_added');
		$this->data['column_action'] = $this->language->get('column_action');

		$this->data['button_delete'] = $this->language->get('button_delete');
		$this->data['button_filter'] = $this->language->get('button_filter');

		$this->data['token'] = $this->session->data['token'];

		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];

			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$this->data['sort_booking'] = $this->url->link('restaurant/booking', 'token=' . $this->session->data['token'] . '&sort=rb.booking_id' . $this->url('rb.booking_id'), 'SSL');
		$this->data['sort_customer'] = $this->url->link('restaurant/booking', 'token=' . $this->session->data['token'] . '&sort=rb.customer_id' . $this->url('rb.customer_id'), 'SSL');
		$this->data['sort_email'] = $this->url->link('restaurant/booking', 'token=' . $this->session->data['token'] . '&sort=rb.email' . $this->url('rb.email'), 'SSL');
		$this->data['sort_status'] = $this->url->link('restaurant/booking', 'token=' . $this->session->data['token'] . '&sort=rb.booking_status_id' . $this->url('rb.booking_status_id'), 'SSL');
		$this->data['sort_date_booked'] = $this->url->link('restaurant/booking', 'token=' . $this->session->data['token'] . '&sort=rb.date_booked' . $this->url('rb.date_booked'), 'SSL');
		$this->data['sort_date_added'] = $this->url->link('restaurant/booking', 'token=' . $this->session->data['token'] . '&sort=rb.date_added' . $this->url('rb.date_added'), 'SSL');

		$pagination = new Pagination();
		$pagination->total = $booking_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('restaurant/booking', 'token=' . $this->session->data['token'] . $this->url(), 'SSL');

		$this->data['pagination'] = $pagination->render();

		$this->data['filter_booking_id'] = $filter_booking_id;
		$this->data['filter_customer'] = $filter_customer;
		$this->data['filter_email'] = $filter_email;
		$this->data['filter_telephone'] = $filter_telephone;
		$this->data['filter_booking_status_id'] = $filter_booking_status_id;
		$this->data['filter_date_booked'] = $filter_date_booked;
		$this->data['filter_date_added'] = $filter_date_added;

		$this->data['sort'] = $sort;
		$this->data['order'] = $order;

		$this->template = 'restaurant/booking_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
		
		$this->response->setOutput($this->render());
  	}

  	public function getForm() {
		$this->data['heading_title'] = $this->language->get('heading_title');
		 
		$this->data['text_success'] = $this->language->get('text_success');
		$this->data['text_booking_id'] = $this->language->get('text_booking_id');
		$this->data['text_customer_name'] = $this->language->get('text_customer_name');
		$this->data['text_ip'] = $this->language->get('text_ip');
		$this->data['text_request_detail'] = $this->language->get('text_request_detail');
		
		$this->data['entry_number_of_people'] = $this->language->get('entry_number_of_people');
		$this->data['entry_date_booked'] = $this->language->get('entry_date_booked');
		$this->data['entry_email'] = $this->language->get('entry_email');
		$this->data['entry_telephone'] = $this->language->get('entry_telephone');
		$this->data['entry_comment'] = $this->language->get('entry_comment');
		$this->data['entry_booking_status'] = $this->language->get('entry_booking_status');
		
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');

		$this->data['token'] = $this->session->data['token'];

 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
 		if (isset($this->error['permission'])) {
			$this->data['error_permission'] = $this->error['permission'];
		} else {
			$this->data['error_permission'] = '';
		}
  	
 		if (isset($this->error['not_found'])) {
			$this->data['error_not_found'] = $this->error['not_found'];
		} else {
			$this->data['error_not_found'] = '';
		}
		
 		if (isset($this->error['number_of_people'])) {
			$this->data['error_number_of_people'] = $this->error['number_of_people'];
		} else {
			$this->data['error_number_of_people'] = '';
		}
		
 		if (isset($this->error['email'])) {
			$this->data['error_email'] = $this->error['email'];
		} else {
			$this->data['error_email'] = '';
		}
		
 		if (isset($this->error['telephone'])) {
			$this->data['error_telephone'] = $this->error['telephone'];
		} else {
			$this->data['error_telephone'] = '';
		}

 		if (isset($this->error['date_booked'])) {
			$this->data['error_date_booked'] = $this->error['date_booked'];
		} else {
			$this->data['error_date_booked'] = '';
		}
				
		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => false
		);

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('restaurant/booking', 'token=' . $this->session->data['token'], 'SSL'),				
			'separator' => ' :: '
		);

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('heading_title_edit'),
			'href'      => '#',				
			'separator' => ' :: '
		);
		
		$this->data['action'] = $this->url->link('restaurant/booking/update', 'token=' . $this->session->data['token'] . '&booking_id=' . $this->request->get['booking_id'] . $this->url(), 'SSL');
		
		$this->data['cancel'] = $this->url->link('restaurant/booking', 'token=' . $this->session->data['token'] . $this->url(), 'SSL');

    	if (isset($this->request->get['booking_id'])) {
      		$booking_info = $this->model_restaurant_booking->getBooking($this->request->get['booking_id']);
    	}
    	
    	 if (isset($booking_info) && $booking_info['restaurant_id'] != $this->restaurant->getRestaurantId()){
    		$this->data['error_permission'] = $this->language->get('error_permission');
    	 }else if(isset($booking_info)){
			$this->data['booking_info'] = $booking_info;    		
    		
	    	if (isset($this->request->post['number_of_people'])) {
	      		$this->data['number_of_people'] = $this->request->post['number_of_people'];
			} elseif (!empty($booking_info)) { 
				$this->data['number_of_people'] = $booking_info['number_of_people'];
			} else {
	      		$this->data['number_of_people'] = '';
	    	}
	
	    	if (isset($this->request->post['date_booked'])) {
	      		$this->data['date_booked'] = $this->request->post['date_booked'];
	    	} elseif (!empty($booking_info)) { 
				$this->data['date_booked'] = $booking_info['date_booked'];
			} else {
	      		$this->data['date_booked'] = '';
	    	}
	
	    	if (isset($this->request->post['email'])) {
	      		$this->data['email'] = $this->request->post['email'];
	    	} elseif (!empty($booking_info)) { 
				$this->data['email'] = $booking_info['email'];
			} else {
	      		$this->data['email'] = '';
	    	}
					
	    	if (isset($this->request->post['telephone'])) {
	      		$this->data['telephone'] = $this->request->post['telephone'];
	    	} elseif (!empty($booking_info)) { 
				$this->data['telephone'] = $booking_info['telephone'];
			} else {
	      		$this->data['telephone'] = '';
	    	}
				
			$this->load->model('setting/booking_status');
			
			$this->data['booking_statuses'] = $this->model_setting_booking_status->getBookingStatuses();	
					
	    	if (isset($this->request->post['booking_status_id'])) {
	      		$this->data['booking_status_id'] = $this->request->post['booking_status_id'];
	    	} elseif (!empty($booking_info)) { 
				$this->data['booking_status_id'] = $booking_info['booking_status_id'];
			} else {
	      		$this->data['booking_status_id'] = '';
	    	}
			
	    	if (isset($this->request->post['comment'])) {
	      		$this->data['comment'] = $this->request->post['comment'];
	    	} elseif (!empty($booking_info)) { 
				$this->data['comment'] = $booking_info['comment'];
			} else {
	      		$this->data['comment'] = '';
	    	}
    	}else{
    		$this->data['error_not_found'] = $this->language->get('error_not_found');
    	}
		
		$this->template = 'restaurant/booking_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
		
    	$this->response->setOutput($this->render());
  	}
	
  	private function validateForm() {
    	if (!$this->user->hasPermission('modify', 'restaurant/booking')) {
      		$this->error['warning'] = $this->language->get('error_permission');
    	}

    	if ((int)($this->request->post['number_of_people']) == 0) {
      		$this->error['number_of_people'] = $this->language->get('error_number_of_people');
    	}
    	
    	$today = date("Y-m-d");
		$todays_date = strtotime($today);
		
		$date_info = date_parse_from_format('d-m-Y H:i', $this->request->post['date_booked']);
		$date_str = $date_info['year'] . '-' . $date_info['month'] . '-' . $date_info['day'];
		$booking_date = strtotime($date_str);

    	if ((utf8_strlen($this->request->post['date_booked']) == 0) || $booking_date < $todays_date) {
      		$this->error['date_booked'] = $this->language->get('error_date_booked');
    	}

    	if ((utf8_strlen($this->request->post['email']) > 96) || (!preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $this->request->post['email']))) {
      		$this->error['email'] = $this->language->get('error_email');
    	}
		
    	if ((utf8_strlen($this->request->post['telephone']) < 3) || (utf8_strlen($this->request->post['telephone']) > 32)) {
      		$this->error['telephone'] = $this->language->get('error_telephone');
    	}

		if ($this->error && !isset($this->error['warning'])) {
			$this->error['warning'] = $this->language->get('error_warning');
		}
		
		if (!$this->error) {
	  		return true;
		} else {
	  		return false;
		}
  	}    
	
   	private function validateDelete() {
    	if (!$this->user->hasPermission('modify', 'restaurant/booking')) {
			$this->error['warning'] = $this->language->get('error_permission');
    	}

		if (!$this->error) {
	  		return true;
		} else {
	  		return false;
		}
  	}

  	private function url($sort = ''){
		$url = "";

		if (isset($this->request->get['filter_booking_id'])) {
			$url .= '&filter_booking_id=' . $this->request->get['filter_booking_id'];
		}
		
		if (isset($this->request->get['filter_customer'])) {
			$url .= '&filter_customer=' . $this->request->get['filter_customer'];
		}
		
		if (isset($this->request->get['filter_email'])) {
			$url .= '&filter_email=' . $this->request->get['filter_email'];
		}
		
		if (isset($this->request->get['filter_telephone'])) {
			$url .= '&filter_telephone=' . $this->request->get['filter_telephone'];
		}
		
		if (isset($this->request->get['filter_booking_status_id'])) {
			$url .= '&filter_booking_status_id=' . $this->request->get['filter_booking_status_id'];
		}
					
		if (isset($this->request->get['filter_date_added'])) {
			$url .= '&filter_date_added=' . $this->request->get['filter_date_added'];
		}
		
		if (isset($this->request->get['filter_date_booked'])) {
			$url .= '&filter_date_booked=' . $this->request->get['filter_date_booked'];
		}

		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}

		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
		
		if(!empty($sort)) {
			if ($order == 'ASC') {
				$url .= '&order=DESC';
			} else {
				$url .= '&order=ASC';
			}

			$url .= '&sort=' . $sort;
		} else {

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			} else {
				$url .= '&sort=t.name';
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
		}

		$url .= '&page=' . $page;

		return $url;
	}
}
?>